package us.icitap.cm.ui.shared;

import java.io.Serializable;
import java.util.List;

import us.icitap.cm.business.entities.PoliceCode;
import us.icitap.cm.ui.custom.CustButton;
import us.icitap.cm.ui.custom.CustListSelect;
import us.icitap.cm.ui.custom.CustWindow;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Tree;
import com.vaadin.ui.VerticalLayout;

public class PoliceCodeField extends CustomField<List<PoliceCode>> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4786470355649342773L;

	private final CustWindow policeCodeWindow = new CustWindow("Kodi policor");
	
	private final Tree policeCodeTree = new Tree();
	
	private final CustListSelect policeCodeListSelect = new CustListSelect();
	
	private final CustButton addButton = new CustButton("+");
	private final CustButton removeButton = new CustButton("-");
	private final CustButton makePrimaryButton = new CustButton("^");
	
	private  final static BeanItemContainer<PoliceCode> pcContainer = new BeanItemContainer<PoliceCode>(PoliceCode.class);
	private List<PoliceCode> rootPoliceCodeList = null;
	
	public PoliceCodeField(List<PoliceCode> rootPoliceCodeList) {
		this.rootPoliceCodeList = rootPoliceCodeList;
	}
	
	public PoliceCodeField() {
	}
	
	@Override
	protected Component initContent() {
		final HorizontalLayout hl = new HorizontalLayout();
		
		VerticalLayout vl0 = new VerticalLayout();
		vl0.setSizeUndefined();
		vl0.addComponent(policeCodeListSelect);
		vl0.addComponent(this.addButton);
		vl0.addComponent(this.removeButton);
		vl0.addComponent(this.makePrimaryButton);
		
		this.addButton.setWidth("2em");
		this.removeButton.setWidth("2em");
		this.makePrimaryButton.setWidth("2em");
		hl.addComponent(policeCodeListSelect);
		policeCodeListSelect.setContainerDataSource(pcContainer);
		hl.addComponent(vl0);
		hl.setComponentAlignment(vl0, Alignment.TOP_CENTER);
		policeCodeListSelect.setNullSelectionAllowed(false);
		policeCodeListSelect.setWidth("450px");

		VerticalLayout vl = new VerticalLayout();
		vl.addComponent(policeCodeTree);
		policeCodeWindow.setContent(vl);
		policeCodeWindow.setHeight("450px");
		policeCodeWindow.setWidth("500px");
		
		final HierarchicalContainer container;
		
		if(rootPoliceCodeList != null)
			container = new PoliceCodeContainer(rootPoliceCodeList);
		else
			container = new PoliceCodeContainer();
		
		policeCodeTree.setContainerDataSource(container);
		policeCodeTree.setImmediate(true);
		
		this.addButton.addClickListener(new ClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = -8018445249289914747L;

			@Override
			public void buttonClick(ClickEvent event) {
				PoliceCodeField.this.getUI().addWindow(policeCodeWindow);
//				CaseManagementUI.getCurrent().addWindow(policeCodeWindow);
			}
		});
		
		this.removeButton.addClickListener(new ClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = -8616296562886769194L;

			@Override
			public void buttonClick(ClickEvent event) {
				
				pcContainer.removeItem(policeCodeListSelect.getValue());
//				PoliceCodeField.this.setValue(pcContainer.getItemIds());
			}
		});
		
		makePrimaryButton.addClickListener(new ClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 4788634350165187074L;

			@Override
			public void buttonClick(ClickEvent event) {
				Object item = policeCodeListSelect.getValue();
				pcContainer.removeItem(item);
				pcContainer.addItemAt(0, item);
//				PoliceCodeField.this.setValue(pcContainer.getItemIds());
			}
		});
		
		
		policeCodeTree.addItemClickListener(new ItemClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 2658081794686196184L;

			@Override
			public void itemClick(ItemClickEvent event) {
				if(event.isDoubleClick()){
					PoliceCode pc = (PoliceCode) event.getItemId();
					pcContainer.addBean(pc);
//					PoliceCodeField.this.setValue(pcContainer.getItemIds());
					
					
				}
				
			}
		});
		
		return hl;
	}

	
	
	@Override
	public Class<? extends List<PoliceCode>> getType() {
		return (Class<? extends List<PoliceCode>>) List.class;
	}

	@Override
	protected void setInternalValue(List<PoliceCode> newValue) {
		super.setInternalValue(newValue);
		pcContainer.removeAllItems();
		if(newValue != null)
			pcContainer.addAll(newValue);
	}


	@Override
	protected List<PoliceCode> getInternalValue() {
		// TODO Auto-generated method stub
		return pcContainer.getItemIds();
	}




	
}
